/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    execute remote shell script
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.RemoteShellActionType;
import com.hp.snap.gr.utils.CmdUtils;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.SSHTools;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Map;

/**
 * 
 * 
 * Execute remote shell script, The stdout and stderr from result will be saved in action output respectively.
 * 
 * There are some parameter can be configured in the action: host, shell and isFile.
 * 
 * host: remote host ip.
 * Shell: remote shell script.
 * isFile:  mark the shell script is file or not.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua 
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class RemoteShellActionHandler extends AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(RemoteShellActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        String currentSiteName = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
        String correspondSiteName = Utils.getCorrespondSiteName(currentSiteName);
        String sshPath = Constants.SSH_PATH;
        String _sshUser = Utils.getConfigurationValue(Constants.CFG_GRG_ROOT + "/" + correspondSiteName, Constants.CFG_GRG_SITE_SSH_USER);
        String _keyFile = sshPath + File.separator + Constants.SSH_KEY_FILE;
        String _host = Utils.getGRHostIP(correspondSiteName);

        RemoteShellActionType actionType = (RemoteShellActionType) action;
        String command = actionType.getShell();
        boolean _isFile = actionType.isIsFile();
        _host = actionType.getHost() == null ? _host : actionType.getHost();
        if (logger.isDebugEnabled()) {
            logger.debug("invoke the remote shell, remote host:{}, command:{}, isFile:{}", new Object[]{_host, CmdUtils.printableCommand(command), _isFile});
        }
        SSHTools ssh = new SSHTools(_sshUser, _keyFile);
        String result = "";
        try {
            result = ssh.executeRemoteShell(_isFile, command, _host);
            //.cmd(_host, command);
        } catch (Exception e) {
            //e.printStackTrace();
            logger.error(e.toString());
        }
        int status = ssh.getExitStatusOfCommandResult(result);
        if (logger.isDebugEnabled()) {
            logger.debug("execute the remote shell command status is:{}", status);
        }
        String resultStr = ssh.getCommandOutput(result);
        if (logger.isDebugEnabled()) {
            logger.debug("execute the remote shell command output is:{}", resultStr);
        }
        String errStr = ssh.getCommandErr(result);
        if (logger.isDebugEnabled()) {
            logger.debug("execute the remote shell command error is:{}", errStr);
        }
        actionPlanOutputs.put(Constants.REMOTE_SHELL_STDOUT, resultStr);
        actionPlanOutputs.put(Constants.REMOTE_SHELL_STDERR, errStr);
        ssh.closeAll();

    }

}
